<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicForm @register="registerForm">
      <template #remoteSearch="{ model, field }">
        <a-input-search
          v-model:value="model[field]"
          placeholder="请输入公众号名称"
          @search="onSearch"
        />
      </template>
    </BasicForm>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { accountFormSchema } from './account.data';

  import { AccountAdd, AccountEdit, ajaxAccountList } from '/@/api/demo/wechat';

  export default defineComponent({
    name: 'AccountModal',
    components: { BasicModal, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      const rowId = ref('');

      const [registerForm, { setFieldsValue, updateSchema, resetFields, validate }] = useForm({
        labelWidth: 100,
        schemas: accountFormSchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 23,
        },
      });

      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        resetFields();
        setModalProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;

        if (unref(isUpdate)) {
          rowId.value = data.record.id;
          data.record.round_head_img = [data.record.round_head_img];
          setFieldsValue({
            ...data.record,
          });
        }
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增公众号' : '编辑公众号'));
      let accountList = [];
      async function onSearch(value: string) {
        //console.log(value);
        accountList = await ajaxAccountList({ keyword: value });
        //console.log(accountList);
        updateSchema({
          field: 'wechat',
          componentProps: {
            options: accountList,
            labelInValue: true,
          },
        });
      }

      async function handleSubmit() {
        try {
          const values = await validate();

          if (values.wechat) {
            let wechat = values.wechat;
            console.log(accountList);
            console.log(wechat);
            for (let index = 0; index < accountList.length; index++) {
              const element = accountList[index];
              if (element.label == wechat.label) {
                wechat = element;
                break;
              }
            }
            var arrData = {
              nickname: wechat.label,
              //alias: wechat.value,
              appid: wechat.appid,
              head_img: wechat.head_img,
            };
          }
          setModalProps({ confirmLoading: true });
          // TODO custom api
          console.log(values);
          if (Array.isArray(values.head_img)) {
            values.head_img = values.head_img[0];
          }
          if (!unref(isUpdate)) {
            await AccountAdd({ ...arrData });
          } else {
            await AccountEdit({ ...values });
          }
          closeModal();
          emit('success', { isUpdate: unref(isUpdate), values: { ...values, id: rowId.value } });
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return {
        registerModal,
        registerForm,
        getTitle,
        ajaxAccountList,
        onSearch,
        handleSubmit,
      };
    },
  });
</script>
